package com.example.news.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.news.pojo.News;
import com.example.news.pojo.Result;
import com.example.news.service.NewsService;

import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/news")
@RequiredArgsConstructor
public class NewController   {

    @Autowired
    private NewsService newsService;

    /**
     * 分页查询
     */
    @PostMapping("/search")
    public Result search(@RequestBody News news,
                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        Page<News> page = newsService.searchPage(news, pageNum, pageSize);
        return Result.data(page);
    }


    @PostMapping
    public Result save(@RequestBody News news) {
        return newsService.save(news) ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @GetMapping("/{id}")
    public News getById(@PathVariable Long id) {
       return newsService.getById(id);
    }

    //根据ids批量删除数据
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids) {
        return newsService.removeByIds(ids) ? Result.ok("删除成功") : Result.fail("删除失败");
    }


}
